package xyz.hubery.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.hubery.domain.WebConfig;
import xyz.hubery.model.R;
import xyz.hubery.service.WebConfigService;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/webConfigs")
@Api(tags = "资源配置")
public class WebConfigController {

    @Autowired
    private WebConfigService webConfigService;


    @GetMapping
    @ApiOperation(value ="分页查询 webConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "webConfig 的名称") ,
            @ApiImplicitParam(name = "type",value = "webConfig 的类型") , // nteger current ,Integer size
            @ApiImplicitParam(name = "current",value = "当前页") ,
            @ApiImplicitParam(name = "size",value = "每页的大小")
    })
    @PreAuthorize("hasAuthority('web_config_query')")
    public R<Page<WebConfig>> queryPage(Page<WebConfig> page, String title, String type){
        page.addOrder(OrderItem.desc("created"));
        Page<WebConfig> configPage = webConfigService.page(page, new LambdaQueryWrapper<WebConfig>().like(!StringUtils.isEmpty(title), WebConfig::getType, title).eq(!StringUtils.isEmpty(type), WebConfig::getType, type));
        return R.ok(configPage);
    }


    @PostMapping
    @ApiOperation(value = "新增一个 WebConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webConfig" ,value = "webConfig 的 json 数据")
    })
    @PreAuthorize("hasAuthority('web_config_create')")
    public R add(@RequestBody @Validated WebConfig webConfig) {

        boolean save = webConfigService.save(webConfig);
        if(save) {
            return R.ok();
        }
        return R.fail();

    }

    @PatchMapping
    @ApiOperation(value = "修改一个 WebConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webConfig" ,value = "webConfig 的 json 数据")
    })
    @PreAuthorize("hasAuthority('web_config_update')")
    public R update(@RequestBody @Validated WebConfig webConfig){
        boolean b = webConfigService.updateById(webConfig);
        if(b){
            return R.ok();
        }
        return R.fail();
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除 WebConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids" ,value = "要删除的数据的 ids")
    })
    @PreAuthorize("hasAuthority('web_config_delete')")
    public R update(@RequestBody String[] ids) {
        if (ids == null || ids.length == 0) {
            return R.fail("删除时需要给删除数据的 Id");
        }
        boolean b = webConfigService.removeByIds(Arrays.asList(ids));
        if(b){
            return R.ok() ;
        }
        return R.fail("删除失败") ;
    }

    @GetMapping("/banners")
    @ApiOperation(value ="查询所有的 banner")
    public R<List<WebConfig>> listBanners() {
        List<WebConfig> banners = webConfigService.listBanners();
        return R.ok(banners);
    }


}
